
#!/bin/bash

if [ -d "./output" ]; then
	pwd
else
        mkdir ./output
        mkdir ./output/qemu
fi

BUILD_TARGET=$1

PROJECT_ROOT_DIR=$(pwd)
PROJECT_OUTPUT_DIR=$PROJECT_ROOT_DIR/output

QEMU_SOURCE_PATH=$PROJECT_ROOT_DIR/qemu
QEMU_IMAGE_OUTPUT=$PROJECT_OUTPUT_DIR/qemu

KERNEL_SOURCE_PATH=$PROJECT_ROOT_DIR/fossix

build_qemu_defconfig()
{
	echo "configure qemu defconfig"

	cd $QEMU_SOURCE_PATH

	./configure --enable-kvm --enable-debug --target-list=x86_64-softmmu --prefix=$QEMU_IMAGE_OUTPUT
}

build_qemu()
{
	echo "build qemu"

	cd $QEMU_SOURCE_PATH
	
	make -j$nproc

	sudo make install
}

build_kernel()
{
	echo "build kernel"

	cd $KERNEL_SOURCE_PATH

	make
}

build_kernel_menuconfig()
{
	echo "set menuconfig"

	cd $KERNEL_SOURCE_PATH

	make menuconfig
}

build_kernel_clean()
{
        echo "clean kernel"

        cd $KERNEL_SOURCE_PATH

        make clean
}

case "$BUILD_TARGET" in
--help)
	echo_usage
	exit 0
	;;

qemu_defconfig)
	build_qemu_defconfig
	;;

qemu)
	build_qemu
	;;

kernel)
	build_kernel
	;;

kernel_menuconfig)
        build_kernel_menuconfig
        ;;

kernel_clean)
	build_kernel_clean
	;;

*)
	echo "Unknown target: $BUILD_TARGET"
	exit 255
	;;
esac
